package com.kk.miaosha.event;

import com.kk.miaosha.service.ItemKillSuccessService;
import com.kk.miaosha.service.RandomCodeService;
import com.kk.miaosha.utils.MqConstantUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author : K k
 * @date : 21:07 2020/9/26
 */
@Component
@Slf4j
@Transactional
public class EventConsumer implements MqConstantUtil {
    //下单服务
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ItemKillSuccessService iksService;
    @Autowired
    private RandomCodeService randomCodeService;

    /*@RabbitListener(queues = {QUEUE_KILL})
    public void handleCommentMessage(Event event){
        if(event==null){
            log.error("消息格式错误");
            return;
        }
        //构造订单
        SnowflakeGenerator snowflakeGenerator=new SnowflakeGenerator();
        Long id = snowflakeGenerator.next();
        //查询该雪花id是否使用过
        while (randomCodeService.findSnowId(id)){
            //若存在则重新创建
            id=snowflakeGenerator.next();
        }
        //创建新的randomCode
        randomCodeService.save(new RandomCode().setCode(Convert.toStr(id)));
        ItemKillSuccess order=new ItemKillSuccess();
        order.setId(Convert.toStr(id)).setItemId(event.getItemId())
                .setKillId(event.getKillId()).setUserId(event.getUserId());
        //下入数据库
        boolean save = iksService.save(order);
        //TODO 发送短信
        if (save){
            log.info("创建订单成功！");
        }else {
            log.error("创建订单失败！");
        }

    }*/

   /* //监听从延时队列过期发送到死信队列的消息
    @RabbitListener(queues = {MqConstantUtil.DLX_QUEUE})
    public void receive(Map<String,String> map){
        System.out.println("------接收到过期的消息-----："+new Date());
        System.out.println("------接收到的消息内容-----：\n"+map);
        //删除redis中的账单并回滚

    }*/
}
